<?
/** Register class
*	Date:	2011-08-18
*	Author:	terrywu
*/

class Register{	// class start

	private $nmg_table;
	private $facebook_table;
	private $weibo_table;
	private	$openid_table;	
	private	$table;
	private $db;

	public function __construct(){
		global $siteMemberTable;
		$this->nmg_table = $this->exists($siteMemberTable['nmg'])? $siteMemberTable['nmg'] : 'member_nmg';
		$this->facebook_table = $this->exists($siteMemberTable['facebook'])? $siteMemberTable['facebook'] : 'login_facebook';
		$this->weibo_table = $this->exists($siteMemberTable['weibo'])? $siteMemberTable['weibo'] : 'member_weibo';
		$this->openid_table = $this->exists($siteMemberTable['openid'])? $siteMemberTable['openid'] : 'member_openid';
		$this->table = $this->exists($siteMemberTable['main'])? $siteMemberTable['main'] : 'member';
		$this->db = $this->exists($siteMemberTable['db'])? $siteMemberTable['db'] : '';
	}


	/** nmg register
	* @param  Array		$regdata	member表需插入的數據
	* @return Array
	*/
	public function reg_nmg($regdata=array()){

		$email = $regdata['email'];
		$username = $regdata['username'];
		$password = $regdata['password'];
		unset($regdata['password']);

		if($this->exists($email) && $this->exists($username) && $this->exists($password)){

			$email_check = db_query_count($this->table,"email='".addcheck($email)."'",$this->db);
			$username_check = db_query_count($this->table,"username='".addcheck($username)."'",$this->db);

			if($email_check || $username_check){
				return $this->response(false,'101');
			}else{
				$regdata['regtime'] = date('Y-m-d H:i:s',time());
				$regdata['logintime'] = date('Y-m-d H:i:s',time());

				$mid = db_add($this->table,$regdata,$this->db);	# insert to member

				if($mid!=0){
					$salt = randomKey(6);
					$in_data = array();
					$in_data['mid'] = $mid;
					$in_data['username'] = $username;
					$in_data['email'] = $email;
					$in_data['password'] = md5(md5($password).$salt);
					$in_data['salt'] = $salt;
					$in_data['activation'] = 0;
					$in_data['activationtime'] = 0;
					$in_data['regtime'] = date('Y-m-d H:i:s',time());
					$in_data['regip'] = getip();
					$in_data['logintime'] = date('Y-m-d H:i:s',time());
					$newid = db_add($this->nmg_table,$in_data,$this->db); # insert to nmg
				}

				if($mid && $newid){
					return $this->response(true,'100',$mid);
				}else{
					return $this->response(false,'103');
				}			
			}
		}else{
			return $this->response(false,'102');
		}
	}


	/**  facebook reg
	* @param  Array	$regdata	member表需插入的數據
	* @return Array
	*/
	public function reg_facebook($regdata=array()){
		
		$fb_id = $this->exists($regdata['fbid'])? $regdata['fbid'] : 0;
		$username = $regdata['username'];

		$regdata['regtime'] = date('Y-m-d H:i:s',time());
		//$regdata['logintime'] = date('Y-m-d H:i:s',time());
		unset($regdata['fbid']);

		$username_check = db_query_count($this->table,"username='".addcheck($username)."'",$this->db); 
		if($username_check){
			return $this->response(false,'112');
		}

		$mid = db_add($this->table,$regdata,$this->db);	//insert to member

		if($mid){
			$up_data = array();
			$up_data['mid'] = $mid;
			$up_data['regtime'] = date('Y-m-d H:i:s',time());
			//$up_data['logintime'] = time();
			$condition = "fbid='".$fb_id."'";
			$affect = db_update($this->facebook_table,$up_data,$condition,$this->db);
			
			if($affect){
				return $this->response(true,'100',$mid);
			}else{
				return $this->response(false,'111');
			}
		}else{
			return $this->response(false,'111');
		}
	}


	/** weibo reg
	* @param  Array	$data	member表需插入的數據
	* @return Array
	*/
	public function reg_weibo($regdata=array()){
		$wb_id = $this->exists($regdata['wb_id'])? $regdata['wb_id'] : 0;

		$regdata['regtime'] = time();
		$regdata['logintime'] = time();
		unset($regdata['wb_id']);

		$email = $regdata['email'];
		$username = $regdata['username'];

		$email_check = db_query_count($this->table,"email='".addcheck($email)."'",$this->db);
		$username_check = db_query_count($this->table,"username='".addcheck($username)."'",$this->db); 

		if($email_check || $username_check){
			return $this->response(false,'122');
		}

		$mid = db_add($this->table,$regdata,$this->db);	//insert to member

		if($mid){
			$up_data = array();
			$up_data['mid'] = $mid;
			$up_data['regtime'] = time();
			$up_data['logintime'] = time();
			$condition = "wb_id='".$wb_id."'";
			$affect = db_update($this->weibo_table,$up_data,$condition,$this->db);
			
			if($affect){
				return $this->response(true,'100',$mid);
			}else{
				return $this->response(false,'121');
			}
		}else{
			return $this->response(false,'121');
		}
	}


	/**  openid reg
	* @param  Array	$regdata	member表需插入的數據
	* @return Array
	*/
	public function reg_openid($regdata=array()){
		
		$openid_id = $this->exists($regdata['openid_id'])? $regdata['openid_id'] : 0;
		$username = $regdata['username'];

		$regdata['regtime'] = time();
		$regdata['logintime'] = time();
		unset($regdata['openid_id']);

		$username_check = db_query_count($this->table,"username='".addcheck($username)."'",$this->db); 
		if($username_check){
			return $this->response(false,'132');
		}

		$mid = db_add($this->table,$regdata,$this->db);	//insert to member

		if($mid){
			$up_data = array();
			$up_data['mid'] = $mid;
			$up_data['regtime'] = time();
			$up_data['logintime'] = time();
			$condition = "id='".$openid_id."'";
			$affect = db_update($this->openid_table,$up_data,$condition,$this->db);
			
			if($affect){
				return $this->response(true,'100',$mid);
			}else{
				return $this->response(false,'131');
			}
		}else{
			return $this->response(false,'131');
		}
	}


	private function exists($obj){
		return isset($obj) && !empty($obj);
	}


	private function response($status='',$info='',$mid=0){
		return array($status,$info,$mid);
	}

}	// class end
?>